package com.example.tnot.Teacher.CoursePart.CourseInternal.CourseDetailHome.Publish.HomeWorkAndExam;

import android.os.Bundle;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.example.tnot.R;
import com.example.tnot.Teacher.BaseFragment.BaseBackFragment;

import java.util.Objects;

public class SingleFragment extends BaseBackFragment {

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    View view;
    ImageView iv_back;
    TextView tv_head,tv_change;
    Switch sw_change;
    boolean isSingle = true;

    public SingleFragment() {
    }

    public static SingleFragment newInstance(String param1, String param2) {
        SingleFragment fragment = new SingleFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.course_single_fragment, container, false);
        InitUi();
        SwitchChange();
        iv_back = view.findViewById(R.id.iv_back);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController navController = Navigation.findNavController(v);
                navController.navigateUp();
            }
        });
        return view;
    }

    private void InitUi() {
        tv_head = view.findViewById(R.id.tv_head);
        tv_change = view.findViewById(R.id.tv_change);
        assert getArguments() != null;
        String SingleOrMultiple = getArguments().getString("label");
        if (!Objects.equals(SingleOrMultiple, "error") && Objects.equals(SingleOrMultiple, "Single")) {
            tv_head.setText("单选题");
            //单选题
            isSingle = true;
        } else if (Objects.equals(SingleOrMultiple, "multiple")){
            tv_head.setText("多选题");
            //多选题
            isSingle = false;
        }
    }

    private void SwitchChange() {
        sw_change = view.findViewById(R.id.sw_change);

        // 根据当前题型设置 Switch 状态和文本
        if(isSingle){
            //单选题
            sw_change.setChecked(false); // 默认Switch 关闭
            tv_change.setText("切换多选题");
        } else {
            //多选题
            sw_change.setChecked(false); // 默认Switch 开启
            tv_change.setText("切换单选题");
        }

        sw_change.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //点击切换状态
                isSingle = !isSingle;
                if (isChecked && !isSingle) {
                    //单选题中,当Switch开启
                    tv_change.setText("切换单选题");
                    tv_head.setText("多选题");
                    //此时是多选题
                    isSingle = false;

                }else if(!isChecked && isSingle) {
                    //单选题中.当Switch关闭
                    tv_change.setText("切换多选题");
                    tv_head.setText("单选题");
                    //此时是单选题
                    isSingle = true;
                }

                if(isChecked && isSingle) {
                    //多选题,当Switch开启
                    tv_change.setText("切换多选题");
                    tv_head.setText("单选题");
                    //此时是单选题
                    isSingle = true;
                }else if(!isChecked && !isSingle){
                    //多选题,当Switch关闭
                    tv_change.setText("切换单选题");
                    tv_head.setText("多选题");
                    //此时是多选题
                    isSingle = false;
                }
            }
        });
    }
}